<?php

namespace Api\Controller;

use Common\Controller;
use Common\Utils\validate\Validate;

class HomeController extends Controller\ApiController {

    public function getBanner() {
        $model = M('Banner');
        $list = $model->where(array('status' => 1))->order('display desc')->field('image,url')->select();
        $this->output($list);
    }

    public function getShopList() {

        $lat = I('lat');
        $lon = I('lon');
        $lat = is_numeric($lat) ? $lat : '';
        $lon = is_numeric($lon) ? $lon : '';
        $page = (int) I('page');
        $no = (int) I('no');
        $is_credit = (int) I('is_credit');

        $limit = array($page > 1 ? $page : 1, $no > 0 && $no <= 20 ? $no : 10);
        $user_info = $this->getUserInfo();
        
        $user_id = $user_info['user_id'];
        $city_id = $this->getCityId();
        //没登录 没传经纬度就读写缓存
        if (!is_numeric($user_id) && !is_numeric($lat) && !is_numeric($lon)) {
            $cache_key = "HOME_RECOM_SHOP_{$city_id}_{$is_credit}_{$page}_{$no}";
            $list = S($cache_key);
        
            if ($list)
                $this->output($list);
        }
        $list = D('Shops')->getRecomShopList($city_id, $user_id, $limit, $lon, $lat, $is_credit);
        if (isset($cache_key))
            S($cache_key, $list, 300);
        $this->output($list);
    }

    /**
     * @name 获取商家优惠券列表
     */
    public function getBusinessCouponList() {
        //1.接收参数并验证
        $longitude = getParameter('longitude'); //经度
        $latitude = getParameter('latitude'); //纬度
        $page = intval(getParameter('page')); //页码
        $num = intval(getParameter('num')); //每页显示条数
        $is_credit = (int) I('is_credit'); //是否只要含有积分的优惠卷
        $params = array(
            array('key' => 'longitude', 'val' => $longitude, 'require' => false, 'type' => 'pnumber', 'msg' => '经度参数不正确！'),
            array('key' => 'latitude', 'val' => $latitude, 'require' => false, 'type' => 'pnumber', 'msg' => '纬度参数不正确！'),
        );
        $limit = array($page > 1 ? $page : 1, $num > 0 && $num <= 20 ? $num : 10);
        $msg = Validate::checkParams($params);
        if ($msg) {
            $this->error($msg[0]);
        }
        //2.获取用户详情
        $user_info = $this->getUserInfo();
        $user_id = $user_info['user_id']; //用户id
        $city_id = $this->getCityId(); //城市id
        $list = D('Shops')->getBusinessCouponList($city_id, $user_id, $limit, $longitude, $latitude, $is_credit);

        //没登录 没传经纬度就读写缓存
        if (!is_numeric($user_id) && !is_numeric($latitude) && !is_numeric($longitude)) {
            $cache_key = "HOME_RECOM_SHOP_$city_id";
            $list = S($cache_key);
            if ($list)
                $this->output($list);
        }
        if (isset($cache_key))
            S($cache_key, $list, 300);
        $this->output($list);
    }

    public function getUserCouponList() {
        //1.接收参数并验证
        $longitude = getParameter('longitude'); //经度
        $latitude = getParameter('latitude'); //纬度
        $page = intval(getParameter('page')); //页码
        $num = intval(getParameter('num')); //每页显示条数
        $is_credit = (int) I('is_credit'); //是否只要含有积分的优惠卷
        $params = array(
            array('key' => 'longitude', 'val' => $longitude, 'require' => false, 'type' => 'pnumber', 'msg' => '经度参数不正确！'),
            array('key' => 'latitude', 'val' => $latitude, 'require' => false, 'type' => 'pnumber', 'msg' => '纬度参数不正确！'),
        );
        $limit = array($page > 1 ? $page : 1, $num > 0 && $num <= 20 ? $num : 10);
        $msg = Validate::checkParams($params);
        if ($msg) {
            $this->error($msg[0]);
        }
        //2.获取用户详情
        $user_info = $this->getUserInfo();
        $user_id = $user_info['user_id']; //用户id
        $city_id = $this->getCityId(); //城市id
        $list = D('Shops')->getUserCouponList($city_id, $user_id, $limit, $longitude, $latitude, $is_credit);
        $this->output($list);
    }

}
